package com.androidwwf.jwr;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.TextView;

public class PlayerStatistics {

	private final String playerprefs = "playerdata";
	private final String pointskey = "pointskey";

	private final String levelnamekey = "levelname";
	private final String quizplayed = "quizplayed";
	private final String picturesplayed = "picturesplayed";

	private SharedPreferences playerdata;
	private SharedPreferences.Editor pp;

	private Context context;

	private String[] levelnames;
	private String levelname = "";
	private String previouslevelname = "";
	
	private String levelzero = "levelzero";
	private String levelone = "levelone";
	private String leveltwo = "leveltwo";
	private String levelthree = "levelthree";
	private String levelfour = "levelfour";
	private String levelfive = "levelfive";
	private String levelsix = "levelsix";

	public PlayerStatistics(Context c) {
		this.context = c;
		playerdata = context.getSharedPreferences(playerprefs, 0);
		pp = playerdata.edit();

		levelnames = new String[] { "Rookie", "Ranger", "Forest Gump", "Tree Huger", "Expert", "Nature Freak" };
	}

	public synchronized void updatePlayerPoints(int points, String game) {
		int currentpoints = playerdata.getInt(pointskey, 0);
		int amountquizplayed = playerdata.getInt(quizplayed, 0);
		int amountpicturesplayed = playerdata.getInt(picturesplayed, 0);

		if (game == "quiz") {
			amountquizplayed++;
		} else {
			amountpicturesplayed++;
		}

		currentpoints += points;
		pp.putInt(quizplayed, amountquizplayed);
		pp.putInt(picturesplayed, amountpicturesplayed);
		pp.putInt(pointskey, currentpoints);
		pp.commit();
//		checkLevelUp();
	}

	public synchronized void resetPlayerData() {
		int resetvalueint = 0;

		pp.putInt(quizplayed, resetvalueint);
		pp.putInt(picturesplayed, resetvalueint);
		pp.putInt(pointskey, resetvalueint);
		
		pp.putBoolean(levelzero, false);
		pp.putBoolean(levelone, false);
		pp.putBoolean(leveltwo, false);
		pp.putBoolean(levelthree, false);
		pp.putBoolean(levelfour, false);
		pp.putBoolean(levelfive, false);
		pp.putBoolean(levelsix, false);
				
		pp.commit();
		
		levelname = "";
	}

	private synchronized boolean setPlayerLevel() {
		int totalPoints = playerdata.getInt(pointskey, 0);

		boolean lvlzero = playerdata.getBoolean(levelzero, false);
		boolean lvlone = playerdata.getBoolean(levelone, false);
		boolean lvltwo = playerdata.getBoolean(leveltwo, false);
		boolean lvlthree = playerdata.getBoolean(levelthree, false);
		boolean lvlfour = playerdata.getBoolean(levelfour, false);
		boolean lvlfive = playerdata.getBoolean(levelfive, false);
		boolean lvlsix = playerdata.getBoolean(levelsix, false);
		
		
		if (totalPoints > 20 && totalPoints <= 40 && !lvlzero && !lvlone) {

			previouslevelname = playerdata.getString(levelnamekey, "Noob");
			pp.putString(levelnamekey, levelnames[0]);
			pp.putBoolean(levelzero, true);
			pp.putBoolean(levelone, true);
			pp.commit();
			levelname = playerdata.getString(levelnamekey, "Noob");
			return true;

		} else if (totalPoints > 40 && totalPoints <= 60 && !lvltwo) {

			previouslevelname = playerdata.getString(levelnamekey, "Noob");
			pp.putString(levelnamekey, levelnames[1]);
			pp.putBoolean(leveltwo, true);
			pp.commit();
			levelname = playerdata.getString(levelnamekey, "Noob");
			return true;

		} else if (totalPoints > 60 && totalPoints <= 80 && !lvlthree) {
			
			previouslevelname = playerdata.getString(levelnamekey, "Noob");
			pp.putString(levelnamekey, levelnames[2]);
			pp.putBoolean(levelthree, true);
			pp.commit();
			levelname = playerdata.getString(levelnamekey, "Noob");
			return true;

		} else if (totalPoints > 80 && totalPoints <= 100 && !lvlfour) {
			
			previouslevelname = playerdata.getString(levelnamekey, "Noob");
			pp.putString(levelnamekey, levelnames[3]);
			pp.putBoolean(levelfour, true);
			pp.commit();
			levelname = playerdata.getString(levelnamekey, "Noob");
			return true;

		} else if (totalPoints > 100 && totalPoints <= 120 && !lvlfive) {
		
			previouslevelname = playerdata.getString(levelnamekey, "Noob");
			pp.putString(levelnamekey, levelnames[4]);
			pp.putBoolean(levelfive, true);
			pp.commit();
			levelname = playerdata.getString(levelnamekey, "Noob");
			return true;
		
		} else if (totalPoints > 120 && !lvlsix) {
		
			previouslevelname = playerdata.getString(levelnamekey, "Noob");
			pp.putString(levelnamekey, levelnames[5]);
			pp.putBoolean(levelsix, true);
			pp.commit();
			levelname = playerdata.getString(levelnamekey, "Noob");
			return true;
		} 
		else {

			return false;
		}
	}

	public synchronized boolean checkLevelUp() {

		if (setPlayerLevel()) {
//			Dialog levelupdialog = new Dialog(context);
//			TextView tv = new TextView(context);
//			tv.setText("You are promoted from: "+ previouslevelname + " to: "  + levelname);
//			levelupdialog.setTitle("Congratulations. Level Up!!");
//			levelupdialog.setContentView(tv);
//			levelupdialog.show();
			
			
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public String[] getPrevAndCurLevelName()
	{
		return new String[] {previouslevelname, levelname};
	}

	public String getPlayerLevel() {
		return playerdata.getString(levelnamekey, "Noob");
	}

	public int getPlayerQuiz() {
		return playerdata.getInt(quizplayed, 0);
	}

	public int getPlayerPictures() {
		return playerdata.getInt(picturesplayed, 0);
	}

	public int getPlayerPoints() {
		return playerdata.getInt(pointskey, 0);
	}
}
